package DataStructure_ZL.搜索树哈希和mapset.搜索树;

//实现搜索树
public class BinarySearchTree {

    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }
    }

    public TreeNode root;

    //插入元素
    public boolean insert(int key) {
        if (root == null) {
            root = new TreeNode(key);
            return true;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while (cur != null) {
            if (key > cur.key) {
                parent = cur;
                cur = cur.right;
            } else if (key < cur.key) {
                parent = cur;
                cur = cur.left;
            } else {
                return false;
            }
        }
        //到这里说明找到应该插入的位置
        TreeNode iu = new TreeNode(key);
        if (key < parent.key) {
            parent.left = iu;
        } else {
            parent.right = iu;
        }
        return true;
    }

    //查找指定元素
    public TreeNode search(int key) {
        TreeNode cur = root;
        while (cur != null) {
            if (key < cur.key) {
                cur = cur.left;
            } else if (key > cur.key) {
                cur = cur.right;
            } else {
                return cur;
            }
        }
        return null;
    }

    //删除元素
    public boolean remove(int key) {
        if (root == null) return false;
        TreeNode cur = root;
        TreeNode parent=null;
        while(cur!=null){
            if (key==cur.key){
                //调用具体的删除方法
                removeNode(parent,cur);
                return true;
            }else if (key>cur.key){
                parent=cur;
                cur=cur.right;
            }else{
                parent=cur;
                cur=cur.left;
            }
        }
        return false;
    }
    //具体的删除方法
    private void removeNode(TreeNode parent,TreeNode cur){
        //总体分为cur左边为空，右边为空和左右都不为空的情况
        if (cur.left==null){
            //cur为头结点
            if (cur==root){
                root=cur.right;
            }else if (cur==parent.left){
                parent.left=cur.right;
            }else{
                parent.right=cur.right;
            }
        }else if(cur.right == null) {
            if(cur == root) {
                root = cur.left;
            }else if(cur == parent.left) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }else {
            //在要删除的左边找最大值或者右树找最小的值把cur的值替换再把找到的最值删除
            TreeNode targetParent = cur;
            TreeNode target = cur.right;
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            cur.key = target.key;
            if(target == targetParent.left) {
                targetParent.left = target.right;
            }else {
                targetParent.right = target.right;
            }
        }
    }

    //输出有序的数组
    public void inorder(TreeNode root){
        if (root==null)return;
        inorder(root.left);
        System.out.println(root.key+" ");
        inorder(root.right);
    }
}
